package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleRobotManageEnableDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleRobotManageListDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleRobotManageSetDTO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleRobotManageEnableVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleRobotManageListVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleRobotManageSetVO;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BundleErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.DingBundleEntity;
import com.xbongbong.saas.domain.entity.DingRobotManageEntity;
import com.xbongbong.saas.enums.mini.DingRobotManagePushEnum;
import com.xbongbong.saas.model.DingBundleModel;
import com.xbongbong.saas.model.DingRobotManageModel;
import com.xbongbong.saas.service.DingRobotManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020/9/1 9:13 下午
 */
@Service("dingRobotManageService")
public class DingRobotManageServiceImpl implements DingRobotManageService {

    private static final Logger LOG = LoggerFactory.getLogger(DingRobotManageServiceImpl.class);
    
    @Resource
    private DingRobotManageModel dingRobotManageModel;
    @Resource
    private DingBundleModel dingBundleModel;
    
    @Override
    public BundleRobotManageListVO list(BundleRobotManageListDTO bundleRobotManageListDTO) throws XbbException {
        BundleRobotManageListVO bundleRobotManageListVO = new BundleRobotManageListVO();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",bundleRobotManageListDTO.getCorpid());
        param.put("bundleId", bundleRobotManageListDTO.getDataId());
        param.put("del",0);
        List<DingRobotManageEntity> dingRobotManageEntityList = dingRobotManageModel.findEntitys(param);
        List<DingRobotManageEntity> dingRobotManageList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<DingRobotManagePushEnum> managePushEnumList = DingRobotManagePushEnum.getAllEnum();
        if ( CollectionsUtil.isEmpty(dingRobotManageEntityList) ) {
            for ( DingRobotManagePushEnum managePushEnum : managePushEnumList ) {
                DingRobotManageEntity dingRobotManageEntity = new DingRobotManageEntity(managePushEnum,bundleRobotManageListDTO.getDataId());
                dingRobotManageList.add(dingRobotManageEntity);
            }
        } else {
            Map<String, DingRobotManageEntity> dingRobotManageMap = new HashMap<>(dingRobotManageEntityList.size());
            dingRobotManageEntityList.forEach(dingRobotManageEntity -> {
                dingRobotManageMap.put(dingRobotManageEntity.getAlias(),dingRobotManageEntity);
            });
            for ( DingRobotManagePushEnum managePushEnum : managePushEnumList ) {
                DingRobotManageEntity dingRobotManage = dingRobotManageMap.get(managePushEnum.getAlias());
                if ( Objects.isNull(dingRobotManage) ){
                    dingRobotManage = new DingRobotManageEntity(managePushEnum,bundleRobotManageListDTO.getDataId());
                }
                dingRobotManageList.add(dingRobotManage);
            }
        }
        bundleRobotManageListVO.setList(dingRobotManageList);
        return bundleRobotManageListVO;
    }
    
    @Override
    public BundleRobotManageEnableVO enable(BundleRobotManageEnableDTO bundleRobotManageListDTO) throws XbbException {
        DingRobotManagePushEnum dingRobotManagePushEnum = DingRobotManagePushEnum.getByAlias(bundleRobotManageListDTO.getAlias());
        if (Objects.isNull(dingRobotManagePushEnum)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100028);
        }
        DingBundleEntity dingBundleEntity = dingBundleModel.getEntity(bundleRobotManageListDTO.getDataId(),bundleRobotManageListDTO.getCorpid());
        if (Objects.isNull(dingBundleEntity)) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010016);
        }
        DingRobotManageEntity dingRobotManageModelEntity = dingRobotManageModel.getByAlias(bundleRobotManageListDTO.getAlias(), bundleRobotManageListDTO.getDataId());
        if (Objects.isNull(dingRobotManageModelEntity)) {
            dingRobotManageModelEntity = new DingRobotManageEntity();
            dingRobotManageModelEntity.setBundleId(bundleRobotManageListDTO.getDataId());
            dingRobotManageModelEntity.setOpenConversationId(dingBundleEntity.getOpenConversationId());
            dingRobotManageModelEntity.setCorpid(bundleRobotManageListDTO.getCorpid());
            dingRobotManageModelEntity.setEnable(bundleRobotManageListDTO.getEnable());
            dingRobotManageModelEntity.setAlias(dingRobotManagePushEnum.getAlias());
            dingRobotManageModelEntity.setPushTime(dingRobotManagePushEnum.getPushTime());
            dingRobotManageModelEntity.setStatisticsDeadline(dingRobotManagePushEnum.getStaticsTime());
            dingRobotManageModelEntity.setSetUp(dingRobotManagePushEnum.getSetUp());
            dingRobotManageModelEntity.setTitle(dingRobotManagePushEnum.getName());
            dingRobotManageModelEntity.setMemo(dingRobotManagePushEnum.getMemo());
            dingRobotManageModel.insert(dingRobotManageModelEntity);
        } else {
            dingRobotManageModelEntity.setEnable(bundleRobotManageListDTO.getEnable());
            dingRobotManageModel.update(dingRobotManageModelEntity);
        }
        return new BundleRobotManageEnableVO();
    }

    @Override
    public BundleRobotManageSetVO set(BundleRobotManageSetDTO bundleRobotManageSetDTO) throws XbbException {
        DingBundleEntity dingBundleEntity = dingBundleModel.getEntity(bundleRobotManageSetDTO.getDataId(), bundleRobotManageSetDTO.getCorpid());
        if (Objects.isNull(dingBundleEntity)) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010016);
        }
        DingRobotManagePushEnum dingRobotManagePushEnum = DingRobotManagePushEnum.getByAlias(bundleRobotManageSetDTO.getAlias());
        if (Objects.isNull(dingRobotManagePushEnum)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100028);
        }
        DingRobotManageEntity dingRobotManageModelEntity = dingRobotManageModel.getByAlias(bundleRobotManageSetDTO.getAlias(), bundleRobotManageSetDTO.getDataId());
        if (Objects.isNull(dingRobotManageModelEntity)) {
            dingRobotManageModelEntity = new DingRobotManageEntity();
            dingRobotManageModelEntity.setBundleId(bundleRobotManageSetDTO.getDataId());
            dingRobotManageModelEntity.setOpenConversationId(dingBundleEntity.getOpenConversationId());
            dingRobotManageModelEntity.setCorpid(bundleRobotManageSetDTO.getCorpid());
            dingRobotManageModelEntity.setEnable(dingRobotManagePushEnum.getEnable());
            dingRobotManageModelEntity.setAlias(dingRobotManagePushEnum.getAlias());
            dingRobotManageModelEntity.setPushTime(bundleRobotManageSetDTO.getPushTime());
            dingRobotManageModelEntity.setStatisticsDeadline(bundleRobotManageSetDTO.getStatisticsDeadline());
            dingRobotManageModelEntity.setSetUp(dingRobotManagePushEnum.getSetUp());
            dingRobotManageModelEntity.setTitle(dingRobotManagePushEnum.getName());
            dingRobotManageModelEntity.setMemo(dingRobotManagePushEnum.getMemo());
            dingRobotManageModel.insert(dingRobotManageModelEntity);
        } else {
            dingRobotManageModelEntity.setStatisticsDeadline(bundleRobotManageSetDTO.getStatisticsDeadline());
            dingRobotManageModelEntity.setPushTime(bundleRobotManageSetDTO.getPushTime());
            dingRobotManageModel.update(dingRobotManageModelEntity);
        }
        return new BundleRobotManageSetVO();
    }
}
